package com.boss.asset.dao.impl;

import com.boss.asset.dao.ITodoDao;
import com.boss.asset.service.IRightServiceAdpter;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.sys.utils.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by wangwei on 2019/8/23.
 */
@Repository
public class TodoDaoImpl extends BaseDaoImpl implements ITodoDao {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(TodoDaoImpl.class);


    /**
     * 工作流表
     */
    private static final String TABLE_NAME = "xact_ru_task";

    @Autowired
    private IRightServiceAdpter rightServiceAdpter;

    @Override
    public List<Map<String, Object>> getTaskInfoList(Map<String, Object> config, String userguid, String roleguid, String province, String year) throws AppRuntimeException {
        StringBuilder sql = new StringBuilder();
        if (StringUtils.isEmpty(roleguid)){
            sql.append("select t1.task_def_key_ ");
        }else {
            sql.append("select t1.task_def_key_ ||'#").append(roleguid).append("'");
        }
        sql.append(" as menuguid,count(1) count, ")
                .append(" max( t1.create_time_ ) AS maxtime, ")
                .append(" min( t1.create_time_ ) AS mintime ")
                .append(" from ")
                .append(TABLE_NAME).append(" t1 ")
                .append(" where t1.suspension_state_ != 3 ");
        if ("0".equals(config.get("relation"))) {
            sql.append(" and exists(select 1 from ").append(config.get("wftable")).append(" t2 ")
                    .append(" where t1.business_key_ = t2.id ")
                    .append(" and t2.IS_ENABLED = '1' and t2.province = ? ")
                    .append(rightServiceAdpter.getRightSql(userguid, roleguid,province, year, "t2"));
        } else {
            sql.append(" and exists(select 1 from ").append(config.get("wftable")).append(" t2 ").append(" ,").append(config.get("relationtable")).append(" t3 ")
                    .append(" where t1.business_key_ = t2.guid ")
                    .append(" and t2.").append(config.get("wfcolumn")).append(" = t3.").append(config.get("relationcolumn"))
                    .append(" and t2.year = t3.year and t2.province = t3.province and t2.year = ? and t2.province = ? ")
                    .append(rightServiceAdpter.getRightSql(userguid,roleguid,province, year, "t3"));
        }
        sql.append(" ) ").append(" group by t1.task_def_key_ ");
        if (logger.isInfoEnabled()) {
            logger.info("查询代办的task_sql: {}, sql入参: [{},{}]", sql.toString(), year, province);
        }
        List<Map<String, Object>> taskList = queryForList(sql.toString(), new Object[]{year, province});
        return taskList;
    }

    /**
     *
     * @param config
     * @param userguid
     * @param roleguid
     * @param menguid
     * @param province
     * @param year
     * @return
     * @throws AppRuntimeException
     */
    @Override
    public List<Map<String, Object>> getFlowTaskData(Map<String, Object> config, String userguid, String roleguid, String menguid, String province, String year) throws AppRuntimeException {
        StringBuilder sql = new StringBuilder();
        if (StringUtils.isEmpty(roleguid)) {
            sql.append("select t1.task_def_key_ ");
        } else {
            sql.append("select t1.task_def_key_ ||'#").append(roleguid).append("'");
        }
        sql.append(" as menuguid,count(1) count, ")
                .append(" max( t1.create_time_ ) AS maxtime, ")
                .append(" min( t1.create_time_ ) AS mintime ")
                .append(" from ")
                .append(TABLE_NAME).append(" t1 ")
                .append(" where t1.suspension_state_ != 3 ")
                .append(" and t1.task_def_key_ = ? ");
        if ("0".equals(config.get("relation"))) {
            sql.append(" and exists(select 1 from ").append(config.get("wftable")).append(" t2 ")
                    .append(" where t1.business_key_ = t2.id ")
                    .append(" and t2.IS_ENABLED = '1' and t2.MOF_DIV_CODE = ? ")
                    .append(rightServiceAdpter.getRightSql(userguid, roleguid, province, year, "t2"));
        } else {
            sql.append(" and exists(select 1 from ").append(config.get("wftable")).append(" t2 ").append(" ,").append(config.get("relationtable")).append(" t3 ")
                    .append(" where t1.business_key_ = t2.guid ")
                    .append(" and t2.").append(config.get("wfcolumn")).append(" = t3.").append(config.get("relationcolumn"))
                    .append(" and t2.year = t3.year and t2.province = t3.province and t2.year = ? and t2.province = ? ")
                    .append(rightServiceAdpter.getRightSql(userguid, roleguid, province, year, "t3"));
        }
        sql.append(" ) ").append(" group by t1.task_def_key_ ");
        if (logger.isInfoEnabled()) {
            logger.info("查询代办的task_sql: {}, sql入参: [{},{},{}]", sql.toString(), menguid, year, province);
        }
        List<Map<String, Object>> taskList = queryForList(sql.toString(), new Object[]{menguid, province});
        return taskList;
    }

    /**
     *
     * @param config
     * @param userguid
     * @param roleguid
     * @param menguid
     * @param province
     * @param year
     * @return
     * @throws AppRuntimeException
     */
    @Override
    public List<Map<String, Object>> getFlowTaskData(List<String> bizkeys,Map<String, Object> config, String userguid, String roleguid, String menguid, String province, String year) throws AppRuntimeException {
        StringBuilder sql = new StringBuilder();
        //如果角色id不为空。menuguid为 menuguid#roleguid,用于识别多角色一个菜单的问题
        sql.append("select '").append(menguid).append("#").append(roleguid).append("' as menuguid, ");
        sql.append(" count(1) count");
        sql.append(" from ").append(config.get("wftable")).append(" t1 ");
        sql.append(" where t1.IS_ENABLED = '1' and t1.MOF_DIV_CODE = ? ");
        //无明细表关系查询，获取权限数据的sql，根据主表信息直接过滤待办数据
        sql.append(" and ").append(SysUtils.inSql("id",bizkeys))
                    .append(rightServiceAdpter.getRightSql(userguid, roleguid, province, year, "t1"));

        if (logger.isInfoEnabled()) {
            logger.info("查询代办的task_sql: {}, sql入参: [{},{},{}]", sql.toString(), menguid, year, province);
        }
        List<Map<String, Object>> taskList = queryForList(sql.toString(), new Object[]{ province});
        return taskList;
    }
}
